package me.raih.controller.admin.wares;

import me.raih.pojo.po.Admin;
import me.raih.pojo.po.Category;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.CategoryService;
import me.raih.service.WaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author Raih
 * @Time 2020/11/28  13:46
 *
 * 关于管理商品的一些控制
 */
@Controller
@RequestMapping(value = "admin/wares")
public class WaresController {


    @Autowired
    WaresService waresService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ResultInfo resultInfo;
    @Autowired
    PageBean<Wares> pageBean;

    /**
     * 查找商品
     * @param model 传值的工具
     * @return 前台页面
     */
    @RequestMapping(value = "/wares_list")
    public String toWareList(Model model){

        pageBean = waresService.findWaresByPageAndName(0, 10,null,null,null,null);
        //return "forward:/wares/pageWaresQuery";
        model.addAttribute("WARE_PAGE_LIST",pageBean);
        return "admin/wares/waresList";
    }

    /**
     * 添加页面之前，先把添加商品的分类查出来
     * @param model
     * @return 添加商品的页面
     */
    @RequestMapping(value = "/wares_add")
    public String toAdd(Model model){
        //先把菜单商品分类找出来
        List<Category> categoryList = categoryService.findCategory();
        model.addAttribute("CATEGORY_LIST",categoryList);

        return "admin/wares/waresAdd";
    }

    /**
     * 正式添加商品
     * @param wares
     * @param session
     * @return 添加成功与否的回执
     */
    @RequestMapping(value = "/addWares",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo wareAdd(Wares wares, HttpSession session){
        ResultInfo resultInfo = new ResultInfo();
        Admin admin = (Admin)session.getAttribute("ADMIN_LOGIN_SESSION_FLAG");
        //这个创建人就是现在 登录的人
        wares.setArchivist(admin.getName());
        //显然，最后一个修改人也是自己
        wares.setLastArchivist(admin.getName());
        //状态就是正常
        wares.setWaresStatus("正常");
        boolean flag = waresService.canAddWares(wares);
        resultInfo.setFlag(flag);
        if(flag){
            //如果说能够添加成功
            resultInfo.setMessage("添加成功！");
        }else {
            resultInfo.setMessage("该商品已经存在！");
        }
        return resultInfo;
    }

    /**
     *  通过商品no改变商品状态
     * @param no
     * @param status
     * @return 改变状态成功与否
     */
    @RequestMapping(value = "/waresStatusChange",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo changeStatus(@RequestParam("no") String no,@RequestParam("status") String status){

        resultInfo.setFlag(false);
        if(no == null){
            resultInfo.setMessage("出现了错误，请传递正确的ID");
        }else {
            //Wares waresByNo = waresService.findWaresByNo(no);
            boolean flag = waresService.canChangeStatus(no, status);
            //如果更新成功
            if(flag){
                resultInfo.setMessage("更新成功");
                resultInfo.setFlag(true);
            }else {
                resultInfo.setMessage("更新失败");
            }
        }
        return resultInfo;
    }

    /**
     * 编辑商品的前提，通过no将商品获取出来给前台编辑页面
     * @param no
     * @param model
     * @return 商品编辑页面
     */
    @RequestMapping(value = "/waresEdit")
    public String toEditWares(String no,Model model){
        if(no == null){
            return "";
        }else {
            Wares waresByNo = waresService.findWaresByNo(no);
            model.addAttribute("WARES_EDIT_BY_NO",waresByNo);
        }
        return "admin/wares/waresEdit";
    }

    /**
     * 编辑商品 的反馈
     * @param wares
     * @param session
     * @return 编辑商品成功与否
     */
    @RequestMapping(value = "/editWares",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo editWares(Wares wares,HttpSession session){

        Admin admin = (Admin)session.getAttribute("ADMIN_LOGIN_SESSION_FLAG");

        if(wares.getNo() != null){
            wares.setLastArchivist(admin.getName());
            wares.setUpdateTime(new Date());

            boolean flag = waresService.updateWares(wares);

            resultInfo.setFlag(flag);
            if(flag){
                resultInfo.setMessage("修改成功！");
            }else {
                resultInfo.setMessage("修改失败！");
            }
        }else {
            resultInfo.setFlag(false);
            resultInfo.setMessage("传错值了");
        }
        return resultInfo;
    }

    @RequestMapping(value = "/deleteWares")
    @ResponseBody
    public ResultInfo deleteWares(@RequestParam("no") String no){

        boolean flag = waresService.deleteWares(no);
        if(flag){
            resultInfo.setMessage("下架成功!");
        }else {
            resultInfo.setMessage("下架失败！");
        }
        resultInfo.setFlag(flag);
        return resultInfo;
    }



    @RequestMapping(value = "/wares_list_del")
    public String waresDelList(Model model){

        return "admin/wares/waresListDel";
    }


    @RequestMapping(value = "/findWaresListDel",method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo findAllWaresDel(@RequestParam("param") String param,
                                      @RequestParam("currentPage") String currentPage,
                                      @RequestParam("allPage") String allPage){
        ResultInfo resultInfo = new ResultInfo();
        int currentPageInt = 0;
        int allPageInt = 0;
        try {
            currentPageInt = Integer.parseInt(currentPage);
            allPageInt = Integer.parseInt(allPage);
        } catch (NumberFormatException ignored) {
        }

        param = param.replace("共","").replace("页","");

        pageBean = waresService.findDelWaresByPageAndName(currentPageInt,allPageInt,10,param);

        if(pageBean.getList() == null || pageBean.getList().size() == 0){
            resultInfo.setFlag(false);
            resultInfo.setMessage("没有数据可以查询");
        }else {
            resultInfo.setFlag(true);
            resultInfo.setData(pageBean);
        }
        return resultInfo;
    }

    @RequestMapping(value = "/addDelWares")
    @ResponseBody
    public ResultInfo addDelWares(@RequestParam("no") String no, HttpServletRequest request){
        //查出来删除的商品
        Wares wares = waresService.findDelWaresByNo(no);
        //找到修改人
        Admin admin = (Admin)request.getSession().getAttribute("ADMIN_LOGIN_SESSION_FLAG");
        wares.setLastArchivist(admin.getName());
        //尝试添加到商品库中
        boolean flag = waresService.addDelWares(wares);
        resultInfo.setFlag(flag);
        if(flag){
            resultInfo.setMessage("上架成功！");
        }else {
            resultInfo.setMessage("上架失败！");
        }
        return resultInfo;
    }

}
